/***************************************************************************
 *   Copyright (C) 2007 by Alexandre Masselot,,,   *
 *   alex@alex-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "spectrumpeakintensityutilities.h"

namespace bol{

void SpectrumPeakIntensityUtilities::computeIntensityRank(Spectrum<PeakIntensity*>&spectrum){
  //we use an peak object of replace the moz by the intensity to get the sort function from Peak class
  //well, that's crude, but why not?
  size_t n=spectrum.size();
  Spectrum<IntensityPeak*> int2index;
  int2index.resize(n);
  for(size_t i=0; i<n; i++){
    int2index[i]=new IntensityPeak(-spectrum[i]->intensity());
    int2index[i]->setIndex(i);
  }
  double prevInt;
  size_t prevRank;
  sort(int2index.begin(), int2index.end());
  for(size_t i=0; i<n; i++){
     size_t idx= int2index[i]->index();
     if(i==0){
        spectrum[idx]->setIntensityRank(0);
        prevRank=0;
        prevInt=spectrum[idx]->intensity();
     }else{
        if(spectrum[idx]->intensity()==prevInt){
            spectrum[idx]->setIntensityRank(prevRank);
        }else{
            prevRank=i;
            spectrum[idx]->setIntensityRank(i);
            prevInt=spectrum[idx]->intensity();
        }
     }
  }
}
}
